package blogger.client.ui.panels;


import blogger.client.async.Callback;
import blogger.client.async.services.TwitService;
import blogger.client.async.services.TwitServiceAsync;
import blogger.client.eventqueqe.events.TwitAddedEvent;
import blogger.client.model.entity.Message;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.Window;

public class TwitInputPanel extends VerticalPanel{
    private HandlerManager eventBus = null;

    private TextArea textArea;
    private Button tweetButton;

    public TwitInputPanel() {

    }

    public TextArea getTextArea() {
        return textArea;
    }

    public Button getTweetButton() {
        return tweetButton;
    }

    public TwitInputPanel(final HandlerManager eventBus) {

        this.eventBus = eventBus;
        this.setStyleName("input");

        textArea = new TextArea();
        textArea.setCharacterWidth(50);
        textArea.setVisibleLines(3);
        tweetButton = new Button("Tweet");
        tweetButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                TwitServiceAsync twitServiceAsync = GWT.create(TwitService.class);
                final Message m = new Message(textArea.getText());
                twitServiceAsync.twitMessage(m,new Callback<Message>() {
                    @Override
                    public void doResult(Message result) {
                        textArea.setText("");
                        TwitAddedEvent event = new TwitAddedEvent(result);
                        eventBus.fireEvent(event);

                    }
                });
            }
        });

        this.add(textArea);
        this.add(tweetButton);
    }
}
